###
### DECENT LIVING STANDARDS MAPPING 
### 

###
### DESCRIPTIVE GRAPHS 
### 

rm(list=ls())#; .rs.restartR()


##
## PACKAGES --------------------------------------------------------------
##

library(tidyverse)
library(countrycode)
library(stringr)

##
## LOADING DATA ----------------------------------------------------------
##

load(file="Complete DLS data file_regional level.RData")

#> focus only on the last DHS wave
aux1 <- dls_region_urbanrural %>% 
  group_by(region_num_raw,
           country_name) %>% 
  mutate(wave_max=max(wave, na.rm=T)) %>%
  ungroup() %>% 
  filter(wave==wave_max)

##
## DATA PREPARATION ------------------------------------------------------
## 

#> converting countryname to continent/region

aux1$worldregion <- countrycode(aux1$country_name, origin = "country.name", destination = "region")

table(aux1$worldregion, useNA = "always")

##
## BOXPLOTS: DIFFERENCES ACROSS REGIONS IN ACHIEVEMENT OF DLS
## 

g1 <- aux1 %>% 

  gather(contains("dim"), key="dimension", value="share") %>% 
  mutate(order = recode(dimension,
                            "dim1_housing_region"=5L,
                            "dim2_thermal_region"=3L,
                            "dim3_nutrition_region"=10L,
                            "dim4_foodprep_region"=1L,
                            "dim5_water_region"=8L,
                            "dim6_sanitation_region"=4L,
                            "dim7_health_region"=6L,
                            "dim8_education_region"=2L,
                            "dim9_socialconnect_region"=9L,
                            "dim10_physicalconnect_region" = 7L)) %>% 
  mutate(order2 = recode(worldregion, 
                         "East Asia & Pacific" = 3L,
                         "Europe & Central Asia" =6L,
                         "Latin America & Caribbean" = 4L,
                         "Middle East & North Africa" =5L, 
                         "South Asia" = 2L,
                         "Sub-Saharan Africa" = 1L)) %>% 
  mutate(dimension = recode(dimension,
                            "dim1_housing_region"="Housing",
                            "dim2_thermal_region"="Thermal comfort",
                            "dim3_nutrition_region"="Nutrition",
                            "dim4_foodprep_region"="Food preparation",
                            "dim5_water_region"="Water",
                            "dim6_sanitation_region"="Sanitation",
                            "dim7_health_region"="Healthcare",
                            "dim8_education_region"="Education",
                            "dim9_socialconnect_region"="Social connectedness",
                            "dim10_physicalconnect_region" = "Physical connectedness")) %>% 
  ggplot() + 
  geom_boxplot(mapping=aes(y=fct_reorder(dimension, -order), 
                           x=share, 
                           fill=as.factor(rural)),
               outlier.shape = NA,
               alpha=0.7)+
  xlab("% of population in region with DLS achieved")+ylab("")+
  scale_x_continuous(labels=scales::percent)+
  scale_fill_manual(name="",
                    values=c("#0038e0", "#e01409"),
                    labels=c( "Urban", "Rural"))+
  facet_wrap(facets = ~fct_reorder(worldregion, order2))+
  theme_bw()+
  theme(legend.position = "bottom",
        axis.text = element_text(size = 11),
        axis.title.x = element_text(size = 12),
        legend.text = element_text(size = 14),
        strip.text.x = element_text(size = 12))+
  theme(strip.text = element_text(face = "bold", size=11),
        strip.background = element_rect(fill = "White"),
        axis.text=element_text(size=12))
  
g1

ggsave(plot=g1, filename = "supplementary figure s3.png",
       width = 12, height = 8)
ggsave(plot=g1, filename = "supplementary figure s3.svg",
       width = 12, height = 8)
